package com.yjlove.myplay.utils;

import android.graphics.Bitmap;
import android.os.Environment;

import com.yjlove.myplay.bean.ShowImageInfo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class FileAndImageUtil {

    // 创建文件
    public static void createFile(String filePath, String fileName) {
        try {
            File dir = new File(filePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File file = new File(filePath, fileName);
            if (!file.exists()) {

                file.createNewFile();

            } else {
                file.delete();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取文件内容
    public static String readFileContext(String fileFullPath){
        StringBuffer sb = new StringBuffer();
        try {
            String encoding="utf-8";
            File file=new File(fileFullPath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    sb.append(lineTxt);
                }
                read.close();
                return sb.toString();
            }else{
                return "找不到指定的文件";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "读取文件内容出错";
        }
    }

    // 复制图片
    public static void copyImage(Bitmap bm, String filePath, String fileName) {
        try {
            File dir = new File(filePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File newFile = new File(filePath, fileName);
            if (newFile.exists()) {
                newFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(newFile);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 搜索全路径的图片
    public static ArrayList<ShowImageInfo> getImagesFullPath(String filePath) {
        ArrayList<ShowImageInfo> result = new ArrayList<>();
        File dir = new File(filePath);
        if (dir != null && dir.isDirectory()) {
            File[] files = dir.listFiles();
            try {
                for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    if (file.getName().contains(".jpg")) {
                        ShowImageInfo showImageInfo = new ShowImageInfo();
                        showImageInfo.setImageName(file.getName());
                        showImageInfo.setImagePath(filePath);
                        result.add(showImageInfo);
                    }
                }
            } catch (Exception el) {
                el.printStackTrace();
            }
        } else {
            System.out.println("没有路径");
        }
        return result;
    }

    // 删除图片
    public static void deleteImageByName(String filePath, String filename) {
        File path = new File(filePath);
        if (!path.exists()) {
            return;
        }
        for (File file : path.listFiles()) {
            String name = file.getName();
            if (name.equals(filename)) {
                file.delete();
            }
        }
    }

}
